package com.magic.himalaya.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.magic.himalaya.R;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @create: Create by Magic on 2022/1/28.
 */
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ItemHolder> {
    private static final String TAG = SearchAdapter.class.getSimpleName();
    private List<Album> albumList = new ArrayList<>();

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View item = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend_recyclerview, parent, false);
        return new ItemHolder(item);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
        //封装数据,设置数据
        holder.itemView.setTag(position);
        holder.setData(albumList.get(position));

        //设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (onCallBack != null)
                onCallBack.onClickToDetailActivity(albumList.get(position), position);
        });
    }

    @Override
    public int getItemCount() {
        if (albumList != null)
            return albumList.size();

        return 0;
    }

    public class ItemHolder extends RecyclerView.ViewHolder {

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(Album album) {

            //找到各个控件，设置数据
            ImageView image_small = itemView.findViewById(R.id.cover_url_small);//专辑图片
            TextView text_title = itemView.findViewById(R.id.albumTitle);//专辑标题
            TextView text_intro = itemView.findViewById(R.id.album_intro);//专辑简介
            TextView text_playCount = itemView.findViewById(R.id.playCount);//专辑播放量
            TextView text_includeTrackCount = itemView.findViewById(R.id.includeTrackCount);//专辑总集数量


            if (!album.getCoverUrlSmall().equals("") || album.getCoverUrlSmall().length() > 0) {
                Picasso.get().load(album.getCoverUrlSmall()).into(image_small);
            }
            text_title.setText(album.getAlbumTitle());
            text_intro.setText(album.getAlbumIntro());
            text_playCount.setText(album.getPlayCount() + "播放");
            text_includeTrackCount.setText(" " + album.getIncludeTrackCount() + "集");

        }
    }

    public void setData(List<Album> albumList) {
        this.albumList = albumList;
        notifyDataSetChanged();
    }


    private RecommendListAdapter.OnCallBack onCallBack = null;

    public void setOnCallBack(RecommendListAdapter.OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }

    public interface OnCallBack {
        void onClickToDetailActivity(Album album, int position);
    }
}
